<template>
  <div class="two-factor-page">
    <ResponsiveNav />

    <div class="page-content">
      <!-- 面包屑导航 -->
      <div class="page-nav">
        <div class="breadcrumb">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/account/security' }">
              <i class="fas fa-chevron-left"></i> {{ $t('返回登录与安全') }}
            </el-breadcrumb-item>
            <el-breadcrumb-item>{{ $t('双重认证') }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
      </div>

      <!-- 主要内容 -->
      <div class="main-card">
        <h1 class="page-title">{{ $t('双重认证') }}</h1>

        <div class="setup-steps">
          <!-- 步骤1：下载验证器 -->
          <div class="step-item">
            <div class="step-number">1</div>
            <div class="step-content">
              <h3>{{ $t('下载谷歌验证器') }}</h3>
              <p>{{ $t('在手机应用商店搜索并下载 Google Authenticator') }}</p>
              <div class="app-stores">
                <a href="https://apps.apple.com/app/google-authenticator/id388497605"
                   target="_blank"
                   class="store-link"
                >
                  <img
                    src="@/assets/images/stores/app-store.png"
                    alt="Download on the App Store"
                  >
                </a>
                <a href="https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2"
                   target="_blank"
                   class="store-link"
                >
                  <img
                    src="@/assets/images/stores/google-play.png"
                    alt="Get it on Google Play"
                  >
                </a>
              </div>
            </div>
          </div>

          <!-- 步骤2：扫描二维码 -->
          <div class="step-item">
            <div class="step-number">2</div>
            <div class="step-content">
              <h3>{{ $t('扫描二维码') }}</h3>
              <p>{{ $t('打开谷歌验证器，扫描下方二维码') }}</p>
              <div class="qr-code-wrapper">
                <img :src="qrCodeUrl" alt="QR Code" class="qr-code">
                <div class="secret-key">
                  <p>{{ $t('无法扫描？使用密钥手动添加：') }}</p>
                  <div class="key-display">
                    <span>{{ secretKey }}</span>
                    <el-button
                      link
                      type="primary"
                      @click="copySecretKey"
                    >
                      {{ $t('复制') }}
                    </el-button>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 步骤3：验证 -->
          <div class="step-item">
            <div class="step-number">3</div>
            <div class="step-content">
              <h3>{{ $t('输入验证码') }}</h3>
              <p>{{ $t('输入谷歌验证器显示的6位验证码') }}</p>
              <div class="verify-input">
                <verification-code-input
                  v-model:code="verificationCode"
                  :has-error="verifyError"
                  @complete="handleVerifyComplete"
                />
                <p v-if="verifyError" class="error-tip">
                  {{ $t('验证码错误，请重新输入') }}
                </p>
              </div>
            </div>
          </div>
        </div>

        <!-- 底部按钮 -->
        <div class="form-actions">
          <el-button @click="handleCancel">{{ $t('取消') }}</el-button>
          <el-button
            type="primary"
            :loading="loading"
            :disabled="!verificationCode || verificationCode.length !== 6"
            @click="handleSubmit"
          >
            {{ $t('确认绑定') }}
          </el-button>
        </div>

        <!-- 备份代码对话框 -->
        <el-dialog
          v-model="showBackupCodes"
          :title="$t('备份恢复代码')"
          width="460px"
          :close-on-click-modal="false"
          class="backup-dialog"
        >
          <div class="backup-content">
            <p class="tip">
              <i class="fas fa-exclamation-circle"></i>
              {{ $t('请妥善保管以下恢复代码，当您无法使用谷歌验证器时可用于登录账户。每个代码只能使用一次。') }}
            </p>
            <div class="codes-grid">
              <div
                v-for="code in backupCodes"
                :key="code"
                class="code-item"
              >
                {{ code }}
              </div>
            </div>
            <div class="backup-actions">
              <el-button @click="downloadBackupCodes">
                <i class="fas fa-download"></i> {{ $t('下载代码') }}
              </el-button>
              <el-button type="primary" @click="handleBackupConfirm">
                 {{ $t('我已保存恢复代码')}}
              </el-button>
            </div>
          </div>
        </el-dialog>
      </div>
    </div>

    <Footer />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import Footer from '@/components/Footer.vue'
import VerificationCodeInput from '../profile/components/VerificationCodeInput.vue'
import {useI18n} from "vue-i18n";
const { t } = useI18n();

const router = useRouter()
const loading = ref(false)
const verificationCode = ref('')
const verifyError = ref(false)
const showBackupCodes = ref(false)

// 模拟数据
const qrCodeUrl = '...'
const secretKey = 'ABCD EFGH IJKL MNOP'
const backupCodes = [
  'ABCD-EFGH-IJKL',
  'MNOP-QRST-UVWX',
  'YZAB-CDEF-GHIJ',
  'KLMN-OPQR-STUV',
  'WXYZ-1234-5678',
  'ABCD-9012-3456',
  'EFGH-7890-1234',
  'IJKL-5678-9012'
]

const copySecretKey = () => {
  navigator.clipboard.writeText(secretKey.replace(/\s/g, ''))
  ElMessage.success(t('密钥已复制'))
}

const handleVerifyComplete = () => {
  verifyError.value = false
}

const handleSubmit = async () => {
  loading.value = true

  try {
    // TODO: 调用验证API
    await new Promise(resolve => setTimeout(resolve, 1000))
    showBackupCodes.value = true
  } catch (error) {
    verifyError.value = true
  } finally {
    loading.value = false
  }
}

const handleCancel = () => {
  router.push('/account/security')
}

const downloadBackupCodes = () => {
  const content = backupCodes.join('\n')
  const blob = new Blob([content], { type: 'text/plain' })
  const url = URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = url
  link.download = 'backup-codes.txt'
  link.click()
  URL.revokeObjectURL(url)
}

const handleBackupConfirm = () => {
  showBackupCodes.value = false
  ElMessage.success('双重认证已开启')
  router.push('/account/security')
}
</script>

<style lang="scss" scoped>
.two-factor-page {
  background-color: #f8f8f8;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.page-content {
  padding: 32px 24px;
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
  flex: 1;
}

.page-nav {
  margin-bottom: 24px;

  .breadcrumb {
    :deep(.el-breadcrumb__item) {
      .el-breadcrumb__inner {
        color: #565656;
        font-weight: normal;
        font-size: 14px;

        &:hover {
          color: #c45500;
        }

        i {
          margin-right: 4px;
          font-size: 12px;
        }
      }
    }
  }
}

.main-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  padding: 32px;
}

.page-title {
  font-size: 24px;
  color: #111;
  font-weight: 500;
  margin-bottom: 32px;
}

.setup-steps {
  max-width: 600px;
  margin: 0 auto;
}

.step-item {
  display: flex;
  gap: 24px;
  padding: 24px 0;
  border-bottom: 1px solid #f0f0f0;

  &:last-child {
    border-bottom: none;
  }

  .step-number {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #1890ff;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 500;
    flex-shrink: 0;
  }

  .step-content {
    flex: 1;

    h3 {
      font-size: 16px;
      color: #111;
      margin-bottom: 8px;
    }

    p {
      color: #666;
      margin-bottom: 16px;
    }
  }
}

.app-stores {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-top: 16px;

  .store-link {
    display: block;
    transition: opacity 0.2s;

    &:hover {
      opacity: 0.8;
    }

    img {
      height: 40px;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }
  }
}

.qr-code-wrapper {
  background: #f8f8f8;
  border-radius: 8px;
  padding: 24px;
  text-align: center;

  .qr-code {
    width: 200px;
    height: 200px;
    margin-bottom: 16px;
  }

  .secret-key {
    p {
      margin-bottom: 8px;
    }

    .key-display {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8px;
      font-family: monospace;
      font-size: 16px;
    }
  }
}

.verify-input {
  max-width: 360px;
  margin: 0 auto;

  .error-tip {
    color: #ff4d4f;
    margin-top: 8px;
    text-align: center;
  }
}

.form-actions {
  margin-top: 40px;
  display: flex;
  justify-content: center;
  gap: 16px;

  .el-button {
    min-width: 120px;
  }
}

.backup-dialog {
  :deep(.el-dialog__body) {
    padding: 0 24px 24px;
  }
}

.backup-content {
  .tip {
    background: #fffbe6;
    border: 1px solid #ffe58f;
    padding: 12px;
    border-radius: 4px;
    color: #d48806;
    margin-bottom: 24px;
    display: flex;
    align-items: flex-start;
    gap: 8px;

    i {
      margin-top: 3px;
    }
  }

  .codes-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    margin-bottom: 24px;

    .code-item {
      background: #f8f8f8;
      padding: 12px;
      border-radius: 4px;
      font-family: monospace;
      text-align: center;
      font-size: 16px;
    }
  }

  .backup-actions {
    display: flex;
    justify-content: center;
    gap: 16px;

    .el-button {
      min-width: 140px;
    }
  }
}

@media (max-width: 768px) {
  .page-content {
    padding: 16px;
  }

  .main-card {
    padding: 24px 16px;
  }

  .page-title {
    font-size: 20px;
    margin-bottom: 24px;
  }

  .step-item {
    padding: 16px 0;
  }

  .qr-code-wrapper {
    padding: 16px;

    .qr-code {
      width: 160px;
      height: 160px;
    }
  }

  .backup-content {
    .codes-grid {
      grid-template-columns: 1fr;
    }

    .backup-actions {
      flex-direction: column;
      gap: 12px;

      .el-button {
        width: 100%;
      }
    }
  }
}
</style>
